package com.bpf.base.controller.system;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bpf.base.util.AjaxUtils;
import com.bpf.base.util.ExcelUitl;
import com.bpf.base.util.MapUtil;
import com.bpf.base.common.Constants;
import com.bpf.base.controller.BaseController;
import com.bpf.base.model.system.TSysOperateLog;
import com.bpf.base.bean.system.TSysOperateLogForm;
import com.bpf.base.service.system.TSysOperateLogService;

/**
 * @date 2016-07-20 09:51:17
 */
@Controller
@RequestMapping("/tSysOperateLog")
public class TSysOperateLogController extends BaseController{
	private Logger logger = LoggerFactory.getLogger(TSysOperateLogController.class);

    @Resource
	private TSysOperateLogService tSysOperateLogService;
	
	

    /**
	 * 根据条件查询
	 * @param params
	 * @return
	 * @throws JSONException
	 */
	@RequestMapping("/getTSysOperateLogList")
	@ResponseBody
	public Map<String, Object> getTSysOperateLogList(HttpServletRequest request, TSysOperateLogForm form,String jsonpCallback){
		Integer pageNum = Integer.parseInt(null == request.getParameter("page") ? 1 + "" : request.getParameter("page").toString());
		Integer pageSize = Integer.parseInt(null == request.getParameter("rows") ? 10 + "" : request.getParameter("rows").toString());
		Map<String, Object> params = new HashMap<String, Object>();
		params = MapUtil.toMap(form);
		params.put("offset", (pageNum-1)*pageSize);
		params.put("limit", pageSize);
		return tSysOperateLogService.getTSysOperateLogList(params);
	}    
  
  	@RequestMapping("/addTSysOperateLog")
	@ResponseBody
	public Map<String, Object> addTSysOperateLog(HttpServletRequest request, TSysOperateLog form,String jsonpCallback) throws UnsupportedEncodingException{
		Map<String, Object> params = new HashMap<String, Object>();
		// 参数检查
		params = MapUtil.toMap(form);
		// 调用service
		if(tSysOperateLogService.addTSysOperateLog(params)>0)
			return AjaxUtils.reponseToJson(Constants.Action.SAVE, true);
		else
			return AjaxUtils.reponseToJson(Constants.Action.SAVE, false);
	}
	
	/**
	 * 更新
	 * @param params
	 * @return
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws JSONException
	 */
	@RequestMapping("/updateTSysOperateLog")
	@ResponseBody
	public Map<String, Object> updateTSysOperateLog( TSysOperateLog form,String jsonpCallback) throws InstantiationException, IllegalAccessException{
		// 参数检查
		TSysOperateLog tSysOperateLog = MapUtil.toObject(TSysOperateLog.class, MapUtil.toMap(form));
		// 调用service
		if(tSysOperateLogService.updateTSysOperateLog(tSysOperateLog) > 0)
			return AjaxUtils.reponseToJson(Constants.Action.UPDATE, true);
		else
			return AjaxUtils.reponseToJson(Constants.Action.UPDATE, false);
	}
	
	/**
	 * 删除
	 * @param params
	 * @return
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws JSONException
	 */
	@RequestMapping("/deleteTSysOperateLog")
	@ResponseBody
	public Map<String, Object> deleteTSysOperateLog(HttpServletRequest request,String jsonpCallback) throws InstantiationException, IllegalAccessException{
		String[] idArray=request.getParameterValues("idArray[]");
		// 调用service
		if(tSysOperateLogService.delTSysOperateLogBat(idArray) > 0){
			return AjaxUtils.reponseToJson(Constants.Action.DELETE, true);
		}else{
			return AjaxUtils.reponseToJson(Constants.Action.DELETE, false);
		}
	}
	
	@RequestMapping("/exporttSysOperateLog")
	public void exportTSysOperateLog(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		OutputStream out = response.getOutputStream();
		String title = "业主信息";
		String[] headers = { "住户编号", "业主姓名", "房产证", "土地证", "性别", "身份证", "联系电话" };
		String[] columns = { "id", "name", "housePropertyCert", "landCert", "gender", "idNo", "mobile" };
		List<TSysOperateLog> houseList = tSysOperateLogService.getAllTSysOperateLog(null);
		ExcelUitl<TSysOperateLog> excelUtil = new ExcelUitl<TSysOperateLog>();
		String pattern = "yyyy-MM-dd HH:mm:dd";
		response.setHeader("Content-Disposition", "attachment;filename=" + new String((title + ".xls").getBytes(), "iso-8859-1"));
		excelUtil.exportoExcel(title, headers, columns, houseList, out, pattern);
	}
	
}
